package com.zry.pda.presenter

import android.content.Intent
import android.support.v7.widget.AppCompatSpinner
import android.util.Log
import android.view.View
import android.widget.AdapterView
import com.zry.pda.R
import com.zry.pda.adapter.ProductNameAdapter
import com.zry.pda.adapter.PutStorageMethodAdapter
import com.zry.pda.been.ProductInfo
import com.zry.pda.been.PutStorageMethod
import com.zry.pda.greendao.entity.OutTreasuryCode
import com.zry.pda.retrofit.ShowUserView
import com.zry.pda.ui.activity.BatchStorageActivity
import com.zry.pda.ui.activity.OutTreasuryActivity
import com.zry.pda.ui.activity.SectionNumberStorageActivity
import com.zry.pda.utils.ACacheUtil
import com.zry.pda.utils.DateTimeUtilsKt
import com.zry.pda.utils.SpinnerUtils
import com.zry.pda.utils.ToastUtil
import kotlinx.android.synthetic.main.activity_out_treasury.*


class OutTreasuryPresenter(private val activity: OutTreasuryActivity) : ShowUserView {

    private var productInfoBeanList: MutableList<ProductInfo>? = null
    private var productInfo: ProductInfo? = null
    private var putStorageMethod: PutStorageMethod? = null

    fun outTreasuryHandlerClick(view: View) {
        when (view.id) {
            R.id.batchOutTreasuryBtn -> {
                val intent = Intent(activity, BatchStorageActivity::class.java)
                intent.putExtra("title", "批量出库")
                intent.putExtra("outStock", activity.outStock)
                intent.putExtra("outTreasuryCode", getOutTreasury("批量出库"))
                activity.startActivity(intent)
            }
            R.id.batchVirtualTrayBtn -> {
                if (putStorageMethod!!.putStorageId == "1") {
                    val intent = Intent(activity, SectionNumberStorageActivity::class.java)
                    intent.putExtra("title", "虚拟托盘")
                    intent.putExtra("outStock", activity.outStock)
                    intent.putExtra("outTreasuryCode", getOutTreasury("虚拟托盘"))
                    activity.startActivity(intent)
                } else {
                    ToastUtil.toast(activity, "虚拟托盘出库只能以单品出库类型出库")
                }
            }
            R.id.sectionNumberOutTreasuryBtn -> {
                val intent = Intent(activity, SectionNumberStorageActivity::class.java)
                intent.putExtra("title", "号段出库")
                intent.putExtra("outStock", activity.outStock)
                intent.putExtra("outTreasuryCode", getOutTreasury("号段出库"))
                activity.startActivity(intent)
            }
        }
    }

    private fun getOutTreasury(title: String): OutTreasuryCode {
        val outTreasuryCode = OutTreasuryCode(
            DateTimeUtilsKt().getNowDateTime("yyyyMMddHHmmss"),
            activity.outStock!!.OutStockID,
            activity.outStock!!.OutStockCode,
            productInfo!!.ProductID,
            productInfo!!.ProductName,
            productInfo!!.PackSpecificationId,
            productInfo!!.PackName,
            activity.outStock!!.CustomerName,
            putStorageMethod!!.putStorageId,
            putStorageMethod!!.putStorageName,
            "", "", "",
            productInfo!!.NetContent,
            productInfo!!.UnitName,
            "",
            title
        )
        Log.e("TAG", outTreasuryCode.toString())
        return outTreasuryCode
    }


    fun getProductList() {
        //获取产品列表
        HttpServer(this).getProductList(ACacheUtil().getLoginInfo().EntID, activity)
        //获取产品规格
        HttpServer(this).getProductList(ACacheUtil().getLoginInfo().EntID, activity)
        //出库方式
        initOutTreasury(activity.outgoingWaySpinner)
    }

    private fun initProductName(productNameSpinner: AppCompatSpinner) {
        val productNameAdapter = ProductNameAdapter(activity, productInfoBeanList!!)
        productNameSpinner.adapter = productNameAdapter
        spinnerItemSelect(productNameSpinner)
    }

    private fun initOutTreasury(outgoingWaySpinner: AppCompatSpinner) {
        val putStorageMethodAdapter = PutStorageMethodAdapter(activity, SpinnerUtils().getOutStorageMethodList())
        outgoingWaySpinner.adapter = putStorageMethodAdapter
        spinnerItemSelect(outgoingWaySpinner)
    }


    private fun spinnerItemSelect(appCompatSpinner: AppCompatSpinner) {
        appCompatSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(p0: AdapterView<*>?, view: View?, position: Int, p3: Long) {
                when (appCompatSpinner) {
                    activity.outProductNameSpinner -> {
                        productInfo = productInfoBeanList!![position]
                    }
                    activity.outgoingWaySpinner -> {
                        putStorageMethod = SpinnerUtils().getOutStorageMethodList()[position]
                    }
                }
            }

            override fun onNothingSelected(p0: AdapterView<*>?) = Unit
        }
    }

    override fun toMainActivity(what: Int, data: Any) {
        when (what) {
            1 -> {
                val productInfoList: MutableList<ProductInfo> = data as MutableList<ProductInfo>
                productInfoBeanList = productInfoList
                initProductName(activity.outProductNameSpinner)
            }
        }
    }
}